package com.zthz.douxing

import com.zthz.douxing.command.DouXingDataVersionCommand
import com.zthz.douxing.command.DouXingDataVersionDeleteCommand
import com.zthz.douxing.utils.MessageUtils
import grails.plugin.gson.converters.GSON

/**
 * 基础数据版本的服务。
 */
class DataVersionController extends BaseController{


    static String DATA_VERSION = "DATA_VERSION"

    def index(){

        render (view:"/dataversion/dataversion")
    }

    /**
     * 获取数据的版本信息。
     * <p>线从缓存取，没有取到再去数据库取。
     * @return
     */
    def list() {

        def list

        list = DataVersion.findAll()

        if(list&&list.size()>0)

            render (["status": "0", "errMsg": "", "data": list]as GSON)

        else
            render (["status": "0", "errMsg": "", "data": []]as GSON)
    }



    def save(DouXingDataVersionCommand ddCommand){

        def body = {command ->
                def dataType = getDataType(ddCommand.dataType)
                def dataVersion  = DataVersion.findByDataType(dataType)
                if(dataVersion){
                    return update(ddCommand,dataVersion)
                }else {

                    return excuteAction(getNewInstance(ddCommand),SAVE,false)
                }
        }
        doAJAXREQUEST(ddCommand,body)
    }


    private update(DouXingDataVersionCommand updateCommand,DataVersion dataVersion){

                if(dataVersion){

                    dataVersion.versionLabel = updateCommand?.versionLabel ?
                                          updateCommand.versionLabel : dataVersion.versionLabel

                    dataVersion.fileUrl = updateCommand?.fileUrl ? updateCommand.fileUrl : dataVersion.fileUrl

                    dataVersion.dataVersion = updateCommand?.dataVersion ?
                                                Long.parseLong(updateCommand.dataVersion) :
                                                dataVersion.dataVersion

                    dataVersion.dataType = updateCommand?.dataType ? getDataType(updateCommand.dataType)
                                                                     :dataVersion.dataType

                    dataVersion.extendNote = updateCommand.extendNote ?  updateCommand.extendNote:
                                                                        dataVersion.extendNote

                    if(dataVersion.save())
                        return MessageUtils.getSuccessfullMsgAsJSON()

                    else
                        return MessageUtils.getCommandErrorMsgAsJSON(errorMessageServicesService.
                                                                        getErrorMsg(dataVersion))
                }else

                    return MessageUtils.getCommandErrorMsgAsJSON(getI18NErrorByCode("command.uid"))

    }

    def delete(DouXingDataVersionDeleteCommand ddCommand){

            def body  = {

                value ->

                    def dataVersion =  DataVersion.findById(Long.parseLong(value.uid))
                    try {
                        render (MessageUtils.getSuccessfullMsgAsStr())
                    }catch (Exception e){
                        log.debug(e.printStackTrace())
                        render (MessageUtils.getCommandErrorMsgAsJSON("delete fails"))
                    }
            }

            doAJAXREQUEST(ddCommand,body)

    }


    private DataVersion getNewInstance(DouXingDataVersionCommand ddCommand){

        DataVersion dataVersion = new DataVersion()
        dataVersion.fileUrl = ddCommand.fileUrl ? ddCommand.fileUrl : dataVersion.fileUrl
        dataVersion.dataVersion = ddCommand.dataVersion ? Long.parseLong(ddCommand.dataVersion) : dataVersion.dataVersion
        dataVersion.dataType = ddCommand.dataType ? getDataType(ddCommand.dataType) : dataVersion.dataType
        dataVersion.versionLabel = ddCommand.versionLabel ? ddCommand.versionLabel : dataVersion.versionLabel
        dataVersion.extendNote = ddCommand.extendNote ? ddCommand.extendNote :  dataVersion.extendNote

        return dataVersion
    }

    private DataType getDataType(String dataType){

        DataType type = null

        switch(dataType){
            case "Application" : type = DataType.Application
                break
            case "TrainSchedule" : type = DataType.TrainSchedule
                break
            case "CityName" : type = DataType.CityName
                break
        }
    }


}
